package com.pedro.streamer.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.pedro.streamer.R;

import java.util.List;

public class SoundSourceTabAdapter extends RecyclerView.Adapter<SoundSourceTabAdapter.TopTabViewHolder> {

    private String TAG = "com.bmi.vcam";
    private List<String> list;
    private Context mContext;
    private TopTabViewHolder lastView;

    private OnTextClickListener clickListener;
    @Override
    public TopTabViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sound_source_recycle_layout, parent, false);
        mContext = parent.getContext();

        return new TopTabViewHolder(view);
    }

    static class TopTabViewHolder extends RecyclerView.ViewHolder {


        TextView titleText;

        public TopTabViewHolder(View view) {
            //父类构造函数
            super(view);
            titleText = view.findViewById(R.id.title_text);

        }

    }

    @Override
    public void onBindViewHolder(@NonNull TopTabViewHolder holder, int position) {


        holder.titleText.setText(list.get(position));


        if (lastView == null){
            lastView = holder;
            holder.titleText.setTextColor(mContext.getResources().getColor(R.color.white));
        }else {
            if (holder == lastView){
                holder.titleText.setTextColor(mContext.getResources().getColor(R.color.normal_tab_color));
            }else {
                holder.titleText.setTextColor(mContext.getResources().getColor(R.color.normal_tab_color));
            }
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (clickListener!=null){
                    lastView.titleText.setTextColor(mContext.getResources().getColor(R.color.normal_tab_color));
                    holder.titleText.setTextColor(mContext.getResources().getColor(R.color.white));
                    clickListener.onTextClick(position);
                    lastView = holder;
                }

            }
        });


    }


    public void setClickListener(OnTextClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    @Override
    public int getItemCount() {
        Log.d(TAG, "getItemCount: "+list.size());

        return list.size();
    }

    public interface OnTextClickListener {
        void onTextClick(int position);
    }

}
